package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.PageUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.monitor.FlowTaskTraceVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.task.FlowTaskFinishedNoticeQueryVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.task.FlowTaskFinishedNoticeVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowTaskFinishedNoticeRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowTaskTraceRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowModelQueryInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowTaskFinishedNotice;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowTaskFinishedNoticeDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: niechanggang
 * @date:2021/9/3 14:52
 * @description:
 */
@Repository
public class FlowTaskFinishedNoticeRepositoryImpl  implements FlowTaskFinishedNoticeRepository {

    private Logger  logger  = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FlowTaskTraceRepository  flowTaskTraceRepository;

    @Autowired
    private FlowTaskFinishedNoticeDAO  flowTaskFinishedNoticeDAO;

    @Override
    public int insert(FlowTaskFinishedNoticeVo flowTaskFinishedNoticeVo) {
        FlowTaskFinishedNotice flowTaskFinishedNotice = new FlowTaskFinishedNotice();
        BeanUtils.copyProperties(flowTaskFinishedNoticeVo, flowTaskFinishedNotice);
        int effectCnt = flowTaskFinishedNoticeDAO.insertSelective(flowTaskFinishedNotice);
        return effectCnt;
    }

    @Override
    public List<FlowTaskFinishedNoticeVo> selectList(FlowTaskFinishedNoticeQueryVo  vo) {
        List<FlowTaskFinishedNoticeVo> flowTaskFinishedNoticeVoList = new ArrayList<>();
        List<FlowTaskFinishedNotice> flowTaskFinishedNoticeList = flowTaskFinishedNoticeDAO.selectList(vo);
        for(FlowTaskFinishedNotice flowTaskFinishedNotice : flowTaskFinishedNoticeList){
            FlowTaskFinishedNoticeVo flowTaskFinishedNoticeVo = new FlowTaskFinishedNoticeVo();
            BeanUtils.copyProperties(flowTaskFinishedNotice, flowTaskFinishedNoticeVo);
            flowTaskFinishedNoticeVoList.add(flowTaskFinishedNoticeVo);
        }
        return flowTaskFinishedNoticeVoList;
    }

    @Override
    public PageInfo<FlowTaskTraceVo> selectPageList(FlowTaskFinishedNoticeQueryVo vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<FlowTaskTraceVo> taskTraceVoList = new ArrayList<>();
        List<FlowTaskFinishedNotice> list  =  flowTaskFinishedNoticeDAO.selectList(vo);
        PageInfo<FlowTaskFinishedNotice> pageInfo = new PageInfo<>(list);
        List<FlowTaskFinishedNotice> noticeList =  pageInfo.getList();
        for(FlowTaskFinishedNotice notice : noticeList){
            String  taskId  =  notice.getTaskId();
            FlowTaskTraceVo flowTaskTraceVo =  flowTaskTraceRepository.getTaskTraceByTaskId(taskId);
            taskTraceVoList.add(flowTaskTraceVo);
        }
        PageInfo<FlowTaskTraceVo> flowTaskTraceVoPageInfo =  new PageInfo<>();
        PageUtils.copyProperties(pageInfo, flowTaskTraceVoPageInfo);
        flowTaskTraceVoPageInfo.setList(taskTraceVoList);
        return flowTaskTraceVoPageInfo;
    }
}
